package com.eurasia.assignment.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import com.eurasia.assignment.controller.dto.JudgeSubmitSearchRequest;
import com.eurasia.assignment.domain.User;
import com.eurasia.assignment.domain.hoj.HOJFullCaseJudgement;
import com.eurasia.assignment.service.port.out.HOJFullCaseJudgementPersistencePort;

import lombok.RequiredArgsConstructor;

/**
 * HOJ提交记录服务
 */
@Service
@RequiredArgsConstructor
public class HOJSubmissionService {
    private static final Logger log = LoggerFactory.getLogger(HOJSubmissionService.class);
    
    private final SecurityContext securityContext;
    private final HOJFullCaseJudgementPersistencePort persistencePort;
    
    /**
     * 获取当前用户的判题提交记录
     * 
     * @param request 查询请求参数
     * @return 分页后的提交记录
     */
    public Page<HOJFullCaseJudgement> getCurrentUserSubmissions(JudgeSubmitSearchRequest request) {
        // 获取当前用户
        User currentUser = securityContext.getCurrentUser();
        if (currentUser == null) {
            log.warn("获取当前用户信息失败，无法查询提交记录");
            throw new IllegalStateException("用户未登录或无法获取用户信息");
        }
        
        log.debug("查询用户[{}]的提交记录，条件：status={}, language={}, page={}, size={}, sortBy={}, sortDirection={}",
                currentUser.getId(), request.getStatus(), request.getLanguage(), 
                request.getPage(), request.getSize(), request.getSortBy(), request.getSortDirection());
        
        // 调用持久层获取分页数据
        Page<HOJFullCaseJudgement> submissionsPage = persistencePort.findSubmissions(
                currentUser.getId(),
                request.getStatus(),
                request.getLanguage(),
                request.getPage(),
                request.getSize(),
                request.getSortBy(),
                request.getSortDirection()
        );
        
        log.debug("查询结果：总数={}, 当前页数据条数={}",
                submissionsPage.getTotalElements(), submissionsPage.getNumberOfElements());
        
        return submissionsPage;
    }
} 